const app = getApp();
Page({
  data: {
    openid: '',
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    TabCur: 0,
    scrollLeft: 0,
  },
  showDialogBtn: function(e) {
    this.setData({
      showModal: true
    })
  },
  inputChange:function(e){
    this.setData({
      feedbackValue: e.detail.value
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function() {},
  /**
   * 隐藏模态对话框
   */
  hideModal: function() {

    this.setData({
      showModal: false
    });
  },
  onCancel: function() {
    console.log("点击取消")
    this.hideModal();
  },
  onConfirm: function(e) {
    const db = wx.cloud.database()
    db.collection('Order').doc(e.target.dataset.id).update({
      data: {
        orderState: '2',
        feedback: this.data.feedbackValue
      }
    })
    wx.showToast({
      title: '提交成功',
      icon: 'success',
      duration: 2000
    })
    this.hideModal();
    this.onShow(e);
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },

  onShow: function(e) {
    Promise.all([new Promise((resolve, reject) => {
      wx.cloud.callFunction({
        name: 'login',
        data: {},
        success: res => {
          this.setData({
            openid: res.result.openid
          })
          resolve(res.result.openid)
        },
        fail: err => {
          reject(err)
        }
      })
    })]).then(r => {
      const db = wx.cloud.database()
      //console.log('用户的openid:' + r)

      db.collection('Order').where({
        _openid: `${r}`
      }).get({
        success: res => {
          //console.log(res.data)
          this.setData({
            listData_all: res.data

          })
        }
      })

      db.collection('Order').where({
        _openid: `${r}`,
        orderState: '0'
      }).get({
        success: res => {
          //console.log(res)

          this.setData({
            listData_ing: res.data
          })
        }
      })

      db.collection('Order').where({
        _openid: `${r}`,
        orderState: '1'
      }).get({
        success: res => {
          //console.log(res)
          this.setData({
            listData_ed: res.data
          })
        }
      })

      db.collection('Order').where({
        _openid: `${r}`,
        orderState: '2'
      }).get({
        success: res => {
          //console.log(res)
          this.setData({
            listData_comp: res.data
          })
        }
      })

    }).catch(re => {
      console.log('[云函数] [login] 调用失败', re)
    });
  },
})